package com.qunar.toku.configuration;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import com.qunar.toku.TokuConst;
import com.qunar.toku.utility.TimeSpanHelper;


public class ServerProfile {
	private String name;
	private String host;
	private int port;
	private int deadTimeout;
	private float weightRate;

	public ServerProfile(Node node) {
		NamedNodeMap attributes = node.getAttributes();
		this.name = attributes.getNamedItem("name").getNodeValue();
		this.host = attributes.getNamedItem("host").getNodeValue();
		this.port = Integer.parseInt(attributes.getNamedItem("port").getNodeValue());
		Node atribute = attributes.getNamedItem("weightRate");
        if (atribute != null) {
            this.weightRate = Float.parseFloat(atribute.getNodeValue().toString());
        } else {
            this.weightRate = 1;
        }
        atribute = node.getParentNode().getAttributes().getNamedItem("deadTimeout");
        if (atribute != null) {
        	//设置最小值为30s
        	int dtime = TimeSpanHelper.getIntFromTimeSpan(atribute.getNodeValue().toString());        	
        	if(dtime < 30000){
        		dtime = 30000;
        	}
            this.deadTimeout = dtime;
        } else {
            this.deadTimeout = TokuConst.DEFAULT_DEAD_TIMEOUT;
        }
	}

	public String getName() {
		return name;
	}

	public String getHost() {
		return host;
	}

	public int getPort() {
		return port;
	}

	public int getDeadTimeout() {
		return deadTimeout;
	}

	public float getWeightRate() {
		return weightRate;
	}

}
